/**
 * @description User Controller
 */

 const {
  createUser,
  getUserInfo,
  updateUser,
  updatePwdFun,
  checkUser,
  queryAll,
  updateData
} = require('../service/user')
const { SuccessModel, ErrorModel } = require('../model/ResModel')
const {
  registerFailInfo,
  unCorrectPwd,
  loginFailInfo,
  loginCheckFailInfo,
  updateFailInfo,
  deleteUserFailInfo,
  queryFailInfo,
  operationFailInfo
} = require('../model/ErrorInfo')

/**
 * 用户注册或登录
 * @param {string} userid 用户ID
 * @param {string} nikename 昵称
 * @param {string} city 城市
 * @param {string} avatar 头像
 */
async function login({ username, password }) {
  const userInfo = await getUserInfo({
    username
  })
  if (userInfo) {
    // 查看密码是否一致
    if (password === userInfo.password) {
      return new SuccessModel(userInfo)
    } else {
      return new ErrorModel({
        code: 500,
        msg: '密码不正确'
      })
    }
  } else {
    return new ErrorModel({
      code: 500,
      msg: '账号不存在'
    })
  }
}

async function register({username, password, cardtype, cardno, phone, realname, pType}) {
  const userObj = {
    username,
    password,
    cardtype,
    cardno,
    phone,
    realname,
    pType,
  }
  console.log(userObj)
  const userInfo = await getUserInfo({
    username
  })
  console.log(userInfo)
  if (userInfo) {
    return new ErrorModel({
      code: 500,
      msg: '账号已存在'
    })
  }
  const userInfo2 = await getUserInfo({
    phone
  })
  if (userInfo2) {
    return new ErrorModel({
      code: 500,
      msg: '该手机号已被注册'
    })
  }
  try {
    let rs = await createUser(userObj)
    return new SuccessModel(rs)
  } catch (ex) {
    return new ErrorModel(registerFailInfo)
  }
}

async function check(params) {
  const userInfo = await checkUser(params)
  console.log(userInfo)
  if (userInfo) {
    return new SuccessModel({
      code: 200,
      msg: true
    })
  } else {
    return new SuccessModel({
      code: 200,
      msg: false
    })
  }
}

/**
 * 查询所有用户（支持条件查询和模糊查询）
 * @param {string} nikename 用户昵称 
 * @param {string} userid 用户id 
 * @param {number} page 当前页码
 * @param {number} pageSize 每页条数 
 * @returns 
 */
async function queryAllUser({ page, pageSize, id }) {
  try {
    const result = await queryAll({ page, pageSize, id })
    return new SuccessModel(result)
  } catch (ex) {
    console.error(ex.message, ex.stack)
    return new ErrorModel(queryFailInfo)
  }
}

/**
 * 修改用户信息
 * @param {object} updateInfo 要修改的信息
 * @param {objcet} whereInfo 查询条件
 */
async function updateUserInfo(params, id) {
  try {
    await updateData(params, id)
    return new SuccessModel(params)
  } catch (ex) {
    return new ErrorModel({
      code: '500',
      msg: '当前手机号已被注册'
    })
  }
}
async function updatePwd(params) {
  try {
    let rs = await updatePwdFun(params)
    if (rs) {
      return new SuccessModel(params)
    } else {
      return new ErrorModel({
        msg: '该手机号未注册'
      })
    }
  } catch (ex) {
    return new ErrorModel({})
  }
}
module.exports = {
  login,
  register,
  queryAllUser,
  updateUserInfo,
  check,
  updatePwd
}
